package cn.sanshuilin.kst.system.provider.write.dao;

import cn.sanshuilin.kst.system.provider.bean.Supply;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.List;

/**
 * @author greenlee
 * @version v1.0
 * @create 创建时间: 2017-12-01 22:56
 * Copyright 2017  by 三水林科技有限公司
 **/
@Repository
public class SupplyReq extends AbstractJpaDao<Supply> {


    public List<Supply> getSupplys(int nPageIndex, int nPageSize, String strKeyword) {
            Query query = getSupplysQuery("c", strKeyword);
            query.setFirstResult(nPageIndex * nPageSize);
            query.setMaxResults(nPageSize);
        List<Supply> list = query.getResultList();
        return list;
    }

    public Object getSupplysCount(String strKeyword) {
            Query query = this.getSupplysQuery(" COUNT(*) ", strKeyword);

        return query.getSingleResult();
    }

    private Query getSupplysQuery(String strChange, String strKeyword) {
        String hql = "SELECT " + strChange + " FROM Supply c WHERE (c.Code LIKE :Keyword OR c.Name LIKE :Keyword) ";

        hql += " ORDER BY c.Code ";

        Query query = entityManager.createNativeQuery(hql);

        query.setParameter("Keyword", "%" + strKeyword + "%");
        return query;
    }



}
